package com.wmm.kafka;

import com.wmm.common.Product;
import com.wmm.repoistry.ProductRepository;
import com.wmm.service.ProductService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class ProductKafka {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductRepository productRepository;

    @KafkaListener(topics = {"product"})
    public void onMessage(ConsumerRecord<String, Integer> consumerRecord) {
        Optional<Integer> optional = Optional.ofNullable(consumerRecord.value());
        if (optional.isPresent()) {
            Integer msg = optional.get();
            System.out.println("msg = " + msg);
            Product product = productService.queryById(msg);
            System.out.println("product = " + product);
            productRepository.save(product);
            System.out.println("信息提交到ES索引库");
        }
    }
}
